home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacToggleButtonUI.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  337 lines

  1. /*
  2.  * @(#)MacToggleButtonUI.java    1.6 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25.  
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.border.*;
  28. import com.sun.java.swing.plaf.*;
  29. import com.sun.java.swing.plaf.basic.*;
  30. import java.io.Serializable;
  31.  
  32. /**
  33.  * BasicToggleButton implementation
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @version @(#)MacToggleButtonUI.java    1.0 11/24/97
  43.  * @author Symantec
  44.  * @author Levi Brown
  45.  */
  46. public class MacToggleButtonUI extends BasicToggleButtonUI
  47. {
  48. //    protected static final Border defaultToggleBorder = new CompoundBorder(MacBorderFactory.getToggleButtonBorder(),
  49. //                                       BasicMarginBorder.getMarginBorder());
  50.     
  51.     protected static final int defaultTextIconGap = 4;  
  52.     protected final static Insets defaultMargin = new Insets(0, 8, 0, 8);
  53.     protected final static Insets defaultIconMargin = new Insets(1, 1, 1, 1);
  54.     protected final static Insets defaultCombinedMargin = new Insets(2, 4, 2, 4);
  55.     
  56.     protected static Color GSBColor = UIManager.getColor("GrayscaleAppearanceB");
  57.     protected static Color GSWColor = UIManager.getColor("GrayscaleAppearanceW");
  58.     protected static Color GS2Color = UIManager.getColor("GrayscaleAppearance2");
  59.     protected static Color GS3Color = UIManager.getColor("GrayscaleAppearance3");
  60.     protected static Color GS4Color = UIManager.getColor("GrayscaleAppearance4");
  61.     protected static Color GS7Color = UIManager.getColor("GrayscaleAppearance7");
  62.     protected static Color GS9Color = UIManager.getColor("GrayscaleAppearance9");
  63.  
  64.     protected MacButtonListener macButtonListener = new MacButtonListener(null);
  65.  
  66.     protected ActivationHelper activationHelper;
  67.     protected ChangeListener changeListener;
  68.     protected boolean isActive = true;
  69.  
  70.     public static ComponentUI createUI(JComponent b) 
  71.     {
  72.         return new MacToggleButtonUI();
  73.     }
  74.     
  75.     public Insets getDefaultMargin(AbstractButton b)
  76.     {
  77.         String text = b.getText();
  78.         if (text != null && !text.equals("")) {
  79.             if (b.getIcon() != null)
  80.                 return defaultCombinedMargin;
  81.             return defaultMargin;
  82.         } else
  83.             return defaultIconMargin;
  84.     }
  85.     
  86. //    public Border getDefaultBorder(AbstractButton b)
  87. //    {
  88. //        return defaultToggleBorder;
  89. //    }
  90.     
  91.     public int getDefaultTextIconGap(AbstractButton b)
  92.     {
  93.         return defaultTextIconGap;
  94.     }
  95.     
  96.     public Insets getInsets(JComponent c) 
  97.     { 
  98.         Border border = c.getBorder();
  99.         Insets i = border != null? border.getBorderInsets(c) : new Insets(0,0,0,0);
  100.         return i;
  101.     }
  102.     
  103.     public void installUI(JComponent c)
  104.     {
  105.         super.installUI(c);
  106.         
  107.         if (c.getFont() == null || c.getFont() instanceof UIResource)
  108.             c.setFont(UIManager.getFont("Button.font"));
  109.         
  110.         //The Mac doesn't paint the focus...
  111.         ((AbstractButton)c).setFocusPainted(false);
  112.  
  113.         LookAndFeel.installBorder(c,"ToggleButton.border");
  114.    }
  115.     
  116.     public void paint(Graphics g, JComponent c)
  117.     {
  118.         AbstractButton b = (AbstractButton) c;
  119.         ButtonModel model = b.getModel();
  120.         
  121.         if (model.isArmed() && model.isPressed())
  122.         {
  123.             paintButtonPressed(g,b); 
  124.         }
  125.         else
  126.         {
  127.             paintButton(g, b, false);
  128.         }
  129.     }
  130.     
  131.     public boolean isActive()
  132.     {
  133.         return isActive;
  134.     }
  135.  
  136.     protected BasicButtonListener createListener(JComponent c)
  137.     {
  138.         return macButtonListener;
  139.     }
  140.  
  141.     protected void paintButtonPressed(Graphics g, AbstractButton b)
  142.     {
  143.         paintButton(g, b, true);
  144.     }
  145.     
  146.     protected void paintButton(Graphics g, AbstractButton b, boolean isPressed)
  147.     {
  148.         JComponent    c            = (JComponent) b;
  149.         ButtonModel    model        = b.getModel();
  150.         Dimension    size        = b.getSize();
  151.         FontMetrics    fm            = g.getFontMetrics();
  152.         Rectangle    viewRect    = new Rectangle(size);
  153.         Rectangle    iconRect    = new Rectangle();
  154.         Rectangle    textRect    = new Rectangle();
  155.         Color        fillColor;
  156.         Color        textColor;
  157.         
  158.         //If it's enabled
  159.         if(model.isEnabled() && isActive())
  160.         {
  161.             //If it's pressed
  162.             if(isPressed)
  163.             {
  164.                 fillColor = GS9Color;
  165.                 textColor = GSWColor;
  166.             }
  167.             //If it's released
  168.             else
  169.             {
  170.                 //If it's on
  171.                 if(model.isSelected())
  172.                 {
  173.                     fillColor = GS9Color;
  174.                     textColor = GSBColor;
  175.                 }
  176.                 //If it's off
  177.                 else
  178.                 {
  179.                     fillColor = GS3Color;
  180.                     textColor = GSBColor;
  181.                 }
  182.             }
  183.         }
  184.         //If it's disabled
  185.         else
  186.         {
  187.             //If it's on
  188.             if(model.isSelected())
  189.             {
  190.                 fillColor = GS4Color;
  191.                 textColor = GS7Color;
  192.             }
  193.             //If it's off
  194.             else
  195.             {
  196.                 fillColor = GS2Color;
  197.                 textColor = GS7Color;
  198.             }
  199.         }
  200.         
  201.         // Paint background
  202.         g.setColor(fillColor);
  203.         g.fillRect(2,2,size.width-5,size.height-5);
  204.  
  205.         // layout the text and icon
  206.         String text = SwingUtilities.layoutCompoundLabel
  207.         (
  208.             fm, b.getText(), b.getIcon(),
  209.             b.getVerticalAlignment(), b.getHorizontalAlignment(),
  210.             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  211.             viewRect, iconRect, textRect, b.getText() == null ? 0 : getDefaultTextIconGap(b)
  212.         );
  213.     
  214.         //Adjust the rectangles for the Mac
  215.         if(b.getVerticalAlignment() == AbstractButton.CENTER)
  216.         {
  217.             --iconRect.y;
  218.             --textRect.y;
  219.         }
  220.         
  221.          // Draw the icon if needed
  222.          paintIcon(g, b, iconRect, isPressed);
  223.     
  224.         // Draw the Text if needed
  225.          paintText(g, b, textRect, text, textColor);
  226.  
  227.         // Paint the focus if needed (shouldn't be, but leaving in for extensibility)
  228.         if (b.isFocusPainted() && b.hasFocus())
  229.         {
  230.             // paint UI specific focus
  231.             paintFocus(g, size);
  232.         }
  233.     }
  234.  
  235.      protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text, Color textColor)
  236.      {
  237.         // Draw the Text
  238.         if(text != null && !text.equals(""))
  239.         {
  240.             JComponent    c        = (JComponent) b;
  241.             Font        f        = c.getFont();
  242.             g.setFont(f);
  243.             FontMetrics    fm        = g.getFontMetrics();
  244.             
  245.             g.setColor(textColor);
  246.             g.drawString(text, textRect.x, textRect.y + fm.getAscent() + ((fm.getLeading() + 1) / 2));
  247.         }        
  248.      }
  249.      
  250.      protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect, boolean isPressed)
  251.      {
  252.         // Paint the Icon
  253.         if(b.getIcon() != null)
  254.         { 
  255.             JComponent    c        = (JComponent) b;
  256.             ButtonModel    model    = b.getModel();
  257.             Icon        icon    = null;
  258.             
  259.             if (!model.isEnabled())
  260.             {
  261.                 icon = (Icon) b.getDisabledIcon();
  262.             }
  263.             else if (isPressed)
  264.             {
  265.                 icon = (Icon) b.getPressedIcon();
  266.                 
  267.                 // if no pressed icon, draw a darker version of the icon
  268.                 if (icon == null)
  269.                 {
  270.                     icon = (Icon) b.getIcon();
  271.                     if (icon != null && icon instanceof ImageIcon) {
  272.                         icon = new ImageIcon(
  273.                             PressedFilter.createPressedImage(((ImageIcon) icon).getImage()));
  274.                     }
  275.                 }
  276.             }
  277.             else if (model.isSelected())
  278.             {
  279.                 icon = b.getSelectedIcon();
  280.             }
  281.             else if (b.isRolloverEnabled() && model.isRollover())
  282.             {
  283.                 icon = (Icon) b.getRolloverIcon();
  284.             }
  285.         
  286.             if (icon == null)
  287.             {
  288.                 icon = (Icon) b.getIcon();
  289.             }
  290.             
  291.             icon.paintIcon(c, g, iconRect.x, iconRect.y);
  292.         }
  293.     }
  294.  
  295.     protected void paintFocus(Graphics g, Dimension size)
  296.     {
  297.         //Do nothing
  298.     }
  299.  
  300.     protected void installListeners(JComponent c)
  301.     {
  302.         super.installListeners(c);
  303.         
  304.         activationHelper    = new ActivationHelper(c);
  305.         changeListener        = new ChangeListener();
  306.         activationHelper.addPropertyChangeListener(changeListener);
  307.     }
  308.     
  309.     protected void uninstallListeners(JComponent c)
  310.     {
  311.         super.uninstallListeners(c);
  312.         activationHelper.removePropertyChangeListener(changeListener);
  313.         activationHelper = null;
  314.         changeListener = null;
  315.     }
  316.  
  317.     class ChangeListener implements java.beans.PropertyChangeListener
  318.     {
  319.         public void propertyChange(java.beans.PropertyChangeEvent evt)
  320.         {
  321.             if(evt.getPropertyName().equals("activated"))
  322.             {
  323.                 boolean oldActive = isActive;
  324.                 isActive = ((Boolean) evt.getNewValue()).booleanValue();
  325.                 if(isActive != oldActive)
  326.                 {
  327.                     Object obj = evt.getSource();
  328.                     if(obj instanceof ActivationHelper)
  329.                     {
  330.                         ((ActivationHelper)obj).getComponent().repaint();
  331.                     }
  332.                 }
  333.             }
  334.         }
  335.     }
  336. }
  337.